<?php
/**
 * Created by PhpStorm.
 * User: wuchunyin
 * Date: 2018/11/8
 * Time: 13:36
 */

namespace app\index\controller;
use app\index\base_controller\BaseController;
use app\index\table\BuildingAlterTable;
use app\index\table\BuildingTable;
use think\Db;
use think\Exception;

class Building extends BaseController{

    public function buildingTest(){
        $data = BuildingTable::table()->find();
        dump($data);
    }

    /**
     * page--基本信息
     */
    public function buildingList(){
        return view('building_list');
    }

    /**
     * action--获取信息
     */
    public function getBuildingList(){
        $limit = input('limit');
        $offset = input('offset');
        $sort = input('sort');
        $order = input('order');
        $sortStr = "$sort $order";
        if(empty($sort)){
            $sortStr = 'BUILDING_NO desc';
        }

        $condition = BuildingTable::getSessionConditions();
        try{
            $listSql = BuildingTable::table()->where($condition)->order($sortStr)->limit($offset,$limit)->fetchSql()->select();
            $list = Db::query($listSql);
            $totalSql = BuildingTable::table()->where($condition)->fetchSql()->count();
            $total = Db::query($totalSql);
            return ['rows'=>$list,'total'=>$total[0]['TP_COUNT']];
        }
        catch (Exception $e){
            return $e->getMessage();
        }
    }

    /**
     * page--新增
     */
    public function addPage(){
        $config = BuildingTable::getConfigAdd();
        $data = null;
        $this->assign('config',$config);
        $this->assign('data',$data);
        $this->view->engine->layout('layout_popup');
        return view('building_add');
    }

    /**
     * action 新增
     */
    public function add() {
        $data = input();
        try{
            BuildingTable::add($data);
            return json_encode(['code'=>'200','msg'=>'操作成功']);
        }
        catch (Exception $e){
            return json_encode(['code'=>'0','msg'=>'操作失败:'.$e->getMessage()]);
        }
    }

    /**
     * page--编辑
     */
    public function editPage(){
        $buildingNo = input('buildingNo');
        $config = BuildingTable::getConfigEdit();
        $data = BuildingTable::get($buildingNo);
        $this->assign('config',$config);
        $this->assign('data',$data);
        $this->view->engine->layout('layout_popup');
        return view('building_edit');
    }

    /**
     * 编辑
     */
    public function edit() {
        $buildingNo = input('BUILDING_NO');
        $data = input();
        try{
            BuildingTable::update(['BUILDING_NO'=>$buildingNo],$data);
            return json_encode(['code'=>'200','msg'=>'操作成功']);
        }
        catch (Exception $e){
            return json_encode(['code'=>'0','msg'=>'操作失败:'.$e->getMessage()]);
        }
    }

    /**
     * page--变动信息
     */
    public function buildingAlterList(){
        return view('building_alter_list');
    }

    /**
     * action--获取变动信息
     */
    public function getAlterList(){
        $limit = input('limit');
        $offset = input('offset');
        $sort = input('sort');
        $order = input('order');
        $sortStr = "$sort $order";
        if(empty($sort)){
            $sortStr = 'ALTER_NO desc';
        }

        $condition = null;
        $listCondition = Login::getListConditionBuildingAlter();
        if (!empty($listCondition['BUILDING_NO'])) {
            $value = $listCondition['BUILDING_NO'];
            $condition[] = ['BUILDING_NO','like',"$value%"];
        }
        if (!empty($listCondition['ALTERTYPE_ID'])) {
            $value = $listCondition['ALTERTYPE_ID'];
            $condition[] = ['ALTERTYPE_ID','=',$value];
        }
        if (!empty($listCondition['ALTER_DATE_START'])) {
            $value = Db::raw("to_date('{$listCondition['ALTER_DATE_START']}','yyyy-mm-dd')");
            $condition[] = ['ALTER_DATE','>=',$value];
        }
        if (!empty($listCondition['ALTER_DATE_END'])) {
            $value = Db::raw("to_date('{$listCondition['ALTER_DATE_END']}','yyyy-mm-dd')");
            $condition[] = ['ALTER_DATE','<=',$value];
        }

        try{
            $listSql = BuildingAlterTable::table()->where($condition)->order($sortStr)->limit($offset,$limit)->fetchSql()->select();
            $list = Db::query($listSql);
            $totalSql = BuildingAlterTable::table()->where($condition)->fetchSql()->count();
            $total = Db::query($totalSql);
            return ['rows'=>$list,'total'=>$total[0]['TP_COUNT']];
        }
        catch (Exception $e){
            return $e->getMessage();
        }
    }

    /**
     * page 新增变动
     */
    public function alterAddPage(){
        $alterTypeId = input('type');
        if(empty($alterTypeId)){
            return '错误的变动类型';
        }
        $this->assign('alterTypeId',$alterTypeId);
        $alterList = Db::table('ASSET_DICT_ALTERTYPE')->select();
        $this->assign('alterList',$alterList);
        $buildingList = BuildingTable::table()->select();
        $this->assign('buildingList',$buildingList);
        $collegeList = Db::table('DICT_COLLEGE')->select();
        $this->assign('collegeList',$collegeList);
        $this->view->engine->layout('layout_popup');
        return view('building_alter_add');
    }

    /**
     * action 新增变动
     */
    public function addAlter(){
        $data = input();
        try{
            BuildingAlterTable::add($data);
            return json_encode(['code'=>'200','msg'=>'操作成功']);
        }
        catch (Exception $e){
            return json_encode(['code'=>'0','msg'=>'操作失败:'.$e->getMessage()]);
        }
    }

    /**
     * action 批量删除变动信息
     */
    public function deleteAlter(){
        $idList = input('idList');
        try{
            Db::startTrans();
            foreach ($idList as $id){
                BuildingAlterTable::delete(['ALTER_NO'=>$id]);
            }
            Db::commit();
            return json_encode(['code'=>'200','msg'=>'操作成功']);
        }
        catch (Exception $e){
            Db::rollback();
            return json_encode(['code'=>'0','msg'=>'操作失败:'.$e->getMessage()]);
        }
    }

    /**
     * page 编辑土地变动
     */
    public function alterEditPage(){
        $alterNo = input('alterNo');
        $alterInfo = BuildingAlterTable::get($alterNo);
        if(empty($alterInfo)){
            return '查询变动信息失败';
        }
        $this->assign('data',$alterInfo);

        $buildingList = BuildingTable::table()->select();
        $this->assign('buildingList',$buildingList);
        $collegeList = Db::table('DICT_COLLEGE')->select();
        $this->assign('collegeList',$collegeList);

        $this->view->engine->layout('layout_popup');
        return view('building_alter_edit');
    }

    /**
     * action 编辑变动
     */
    public function editBuildingAlter(){
        $alterNo = input('ALTER_NO');
        $data = input();
        unset($data['ALTER_NO']);
        try{
            BuildingAlterTable::update(['ALTER_NO'=>$alterNo],$data);
            return json_encode(['code'=>'200','msg'=>'操作成功']);
        }
        catch (Exception $e){
            return json_encode(['code'=>'0','msg'=>'操作失败:'.$e->getMessage()]);
        }
    }



    /**
     * 打印
     */
    public function printPage() {
        $id = input('id');
        $data = BuildingTable::get($id);
        if(empty($data)){
            return '查询房屋、构筑物信息失败';
        }
        $config = BuildingTable::getConfigPrint();
        $this->assign('config',$config);
        $this->assign('data',$data);
        $this->view->engine->layout('layout_popup');
        return view('building_print');
    }
    public function searchPage() {
        $this->view->engine->layout('layout_popup');
        return view('building_search');
    }

    public function alterSearchPage() {
        $this->view->engine->layout('layout_popup');
        return view('building_alter_search');
    }
    public function addSession() {
        $condition = input();
        Login::setListConditionBuilding($condition);
        return json_encode(['code'=>'200']);
    }
    public function addSessionAlter() {
        $condition = input();
        Login::setListConditionBuildingAlter($condition);
        return json_encode(['code'=>'200']);
    }

    public function importPage() {
        $this->view->engine->layout('layout_popup');
        return view('import');
    }

    public function import() {
        $inputData = input('data');
        $postData = base64_decode($inputData);
        $list = json_decode($postData,true);
        $start = input('start');

        Db::startTrans();
        $fieldList = BuildingTable::BUILDING_FIELD_LIST;
        $fieldCount = count($fieldList);
        foreach ($list as $key => $row) {
            $data = [];

            // 字段赋值
            $i = 0;
            foreach ($row as $key1=>$value) {
                $data[$fieldList[$i]] = $value;
                $i++;
                if ($i>=$fieldCount) {
                    break;
                }
            }

            try{
                BuildingTable::addImport($data);
            }
            catch (Exception $e){
                Db::rollback();
                return json_encode(['code'=>'0','msg'=>"第".($key + $start)."行数据写入出错",'data'=>$e->getMessage()]);
            }
        }

        Db::commit();
        return json_encode(['code'=>'200','msg'=>'导入成功']);
    }

    /**
     * page 房屋、构筑物导出页面
     */
    public function exportExcelPage() {
        $condition = BuildingTable::getSessionConditions();

        try{
            $totalSql = BuildingTable::table()->where($condition)->fetchSql()->count();
            $total = Db::query($totalSql);
            $total = $total[0]['TP_COUNT'];
            $this->assign('total',$total);

            $arr = $this->getDownLoadList($total);
            $this->assign('downList',$arr);

            $this->view->engine->layout('layout_popup');
            return view('export_excel');
        }
        catch (Exception $e){
            return $e->getMessage();
        }
    }

    /**
     * page 房屋、构筑物导出Excel
     */
    public function exportExcel(){
        $sortStr = 'BUILDING_NO desc';

        $start = input('start');
        $startL = $start-1;
        $end = input('end');
        $endL = $end-$start+1;

        $condition = BuildingTable::getSessionConditions();

        try{
            $listSql = BuildingTable::table()->where($condition)->limit($startL,$endL)->order($sortStr)->fetchSql()->select();
            $list = Db::query($listSql);
            BuildingTable::exportExcel($list,"房屋、构筑物$start-$end");
        }
        catch (Exception $e){
            return $e->getMessage();
        }
    }




}